///*
// *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
// *
// *  Redistribution and use in source and binary forms, with or without
// *  modification, are permitted provided that the following conditions are met:
// *
// *  Redistributions of source code must retain the above copyright notice,
// *  this list of conditions and the following disclaimer.
// *  Redistributions in binary form must reproduce the above copyright
// *  notice, this list of conditions and the following disclaimer in the
// *  documentation and/or other materials provided with the distribution.
// *  Neither the name of the dreamlu.net developer nor the names of its
// *  contributors may be used to endorse or promote products derived from
// *  this software without specific prior written permission.
// *  Author: Chill 庄骞 (smallchill@163.com)
// */
//package com.plian.system.service.dp.impl;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.plian.Tools.*;
//import com.plian.Tools.pagedata.PageData;
//import com.plian.system.constant.sys.FormStatusConstant;
//import com.plian.system.constant.sys.FormTypeConstant;
//import com.plian.system.dto.dp.DisciplineStatisticsDTO;
//import com.plian.system.entity.dp.DisciplineStatistics;
//import com.plian.system.mapper.dp.DisciplineStatisticsMapper;
//import com.plian.system.mybatis.base.BaseFlowService;
//import com.plian.system.mybatis.base.BaseServiceImpl;
//import com.plian.system.service.dp.IDisciplineStatisticsService;
//import com.plian.system.service.sys.code.IFormCodeService;
//import com.plian.system.service.wf.FormWorkflowService;
//import com.plian.system.vo.dp.DisciplineStatisticsVO;
//import com.plian.system.vo.report.HeaderVO;
//import com.plian.system.vo.report.ReportReturnVO;
//import com.plian.system.vo.report.ReportVO;
//import com.plian.system.wrapper.dp.DisciplineStatisticsWrapper;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang.exception.ExceptionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.*;
//
//import static com.plian.system.constant.sys.FormStatusConstant.UNREVIEWED;
//
///**
// * 企业纪检监察干部及监察对象人数 服务实现类
// *
// * @author jianglei
// * @since 2019-12-05
// */
//@Slf4j
//@Service
//@AllArgsConstructor
//public class DisciplineStatisticsServiceImpl extends BaseServiceImpl<DisciplineStatisticsMapper, DisciplineStatistics> implements IDisciplineStatisticsService, BaseFlowService {
//
//	private FormWorkflowService formWorkflowService;
//
//	private IFormCodeService formCodeService;
//
//	private DisciplineStatisticsWrapper disciplineStatisticsWrapper;
//
//	@Override
//	@Transactional(rollbackFor=RuntimeException.class)
//	public boolean saveOrUpdate(DisciplineStatistics disciplineStatistics) {
//		if (disciplineStatistics.getId() == null) {
//			disciplineStatistics.setCode(formCodeService.getCode(FormTypeConstant.DP_DISCIPLINE_STATISTICS_FORM));
//			disciplineStatistics.setFormType(String.valueOf(FormTypeConstant.DP_DISCIPLINE_STATISTICS_FORM));
//			disciplineStatistics.setVersion("1");
//		}
//		if (!Optional.ofNullable(disciplineStatistics.getStatus()).isPresent()) {
//			disciplineStatistics.setStatus(FormStatusConstant.UNREVIEWED);
//		}
//		if (Optional.ofNullable(disciplineStatistics.getFillingTime()).isPresent()){
//			disciplineStatistics.setDef1(DateUtil.format(disciplineStatistics.getFillingTime(), DateUtil.PATTERN_MONTH));
//		}
//		return super.saveOrUpdate(disciplineStatistics);
//	}
//
//	@Override
//	public String checkFillingTime(DisciplineStatistics disciplineStatistics){
//		if (!Optional.ofNullable(disciplineStatistics.getFillingTime()).isPresent()) {
//			return "填报时间为空";
//		}
//		if (!Optional.ofNullable(disciplineStatistics.getFillingUnitId()).isPresent()){
//			return "填报单位为空";
//		}
//		DisciplineStatistics one = getOne(new LambdaQueryWrapper<DisciplineStatistics>().eq(DisciplineStatistics::getFillingUnitId, disciplineStatistics.getFillingUnitId())
//				.eq(DisciplineStatistics::getDef1, DateUtil.format(disciplineStatistics.getFillingTime(), DateUtil.PATTERN_MONTH)));
//		if (!Optional.ofNullable(one).isPresent()){
//			return null;
//		}
//		if (one.getId().equals(disciplineStatistics.getId())){
//			return null;
//		}
//		return "该月份已填报";
//	}
//
//	@Override
//	@Transactional(rollbackFor=RuntimeException.class)
//	public boolean deleteLogic(List<Long> ids) {
//		for (Long id : ids) {
//			DisciplineStatistics disciplineStatistics = getById(id);
//			if (disciplineStatistics == null) {
//				return false;
//			}  else if (disciplineStatistics.getStatus() != null &&
//					(disciplineStatistics.getStatus() != FormStatusConstant.STASH && disciplineStatistics.getStatus() != FormStatusConstant.UNREVIEWED)) {
//				return false;
//			}
//			formWorkflowService.deleteProcessInstance(String.valueOf(id));
//		}
//		return super.deleteLogic(ids);
//	}
//
//	@Override
//	public HashMap<String,Object> commit(String id) {
//		handleStatus(Long.valueOf(id), FormStatusConstant.FLOW);
//
//		//将需要提交的流程变量返回，用于工作流侧
//		return new HashMap<String,Object>();
//	}
//
//	@Override
//	public Boolean finish(String id) {
//		return handleStatus(Long.valueOf(id), FormStatusConstant.FINISH);
//	}
//
//	@Override
//	public Boolean reject(String id, int formState, String comment) {
//		return handleStatus(Long.valueOf(id), formState);
//	}
//
//	@Override
//	public List<PageData> findByIds(List<String> formIds) {
//		QueryWrapper<DisciplineStatistics> queryWrapper = new QueryWrapper<>();
//		queryWrapper.in("id", Func.toLongList(StringUtils.join(formIds, StringPool.COMMA)));
//		List<DisciplineStatistics> disciplineStatistics = list(queryWrapper);
//		List<DisciplineStatisticsVO> disciplineStatisticsVOS = disciplineStatisticsWrapper.entityToVO(disciplineStatistics);
//		List<PageData> pageDataList = new ArrayList<>();
//		try{
//			if (CollectionUtil.isNotEmpty(disciplineStatisticsVOS)){
//				for (DisciplineStatisticsVO vo : disciplineStatisticsVOS){
//					PageData pd = new PageData();
//					pd.put("id", vo.getId());
//					pd.put("UNITNAME", vo.getFillingUnitName());
//					pd.put("APPLICANTNAME", vo.getCreateUserName());
//					pd.put("applicantId", vo.getCreateUser());
//					pd.put("applyOrganizationId", vo.getFillingUnitId());
//					pd.put("formType", FormTypeConstant.DP_DISCIPLINE_STATISTICS_FORM);
//					pd.put("orgName", vo.getFillingUnitName());
//					pageDataList.add(pd);
//				}
//			}
//
//			return pageDataList;
//		} catch (Exception e) {
//			log.error(ExceptionUtils.getFullStackTrace(e));
//		}
//		return pageDataList;
//	}
//
//	@Override
//	public List<Integer> getFormType() {
//		return Arrays.asList(FormTypeConstant.DP_DISCIPLINE_STATISTICS_FORM);
//	}
//
//	@Override
//	public ReportReturnVO summary(DisciplineStatisticsDTO disciplineStatisticsDTO){
//		List<DisciplineStatisticsVO> data = disciplineStatisticsWrapper.entityToVO(baseMapper.summary(disciplineStatisticsDTO.getFillingDate(), disciplineStatisticsDTO.getFillingUnitIds()));
//
//		// 总计
//		if (CollectionUtil.isNotEmpty(data)) {
//			DisciplineStatisticsVO end = new DisciplineStatisticsVO();
//			end.setFillingUnitName("总计：" + data.size());
//			for (int i = 0 ; i <data.size(); i++){
//				DisciplineStatisticsVO vo = data.get(i);
//				end.setDeputyDisciplineSecretary(BigDecimalUtil.integerAdd(end.getDeputyDisciplineSecretary(), vo.getDeputyDisciplineSecretary()));
//				end.setOtherCharge(BigDecimalUtil.integerAdd(end.getOtherCharge(), vo.getOtherCharge()));
//				end.setDisciplineFullTime(BigDecimalUtil.integerAdd(end.getDisciplineFullTime(), vo.getDisciplineFullTime()));
//				end.setDisciplinePartTime(BigDecimalUtil.integerAdd(end.getDisciplinePartTime(), vo.getDisciplinePartTime()));
//				end.setSubtotal(BigDecimalUtil.integerAdd(end.getSubtotal(), vo.getSubtotal()));
//				end.setCityTubeCadre(BigDecimalUtil.integerAdd(end.getCityTubeCadre(), vo.getCityTubeCadre()));
//				end.setTeamMembers(BigDecimalUtil.integerAdd(end.getTeamMembers(), vo.getTeamMembers()));
//				end.setCorporateMiddle(BigDecimalUtil.integerAdd(end.getCorporateMiddle(), vo.getCorporateMiddle()));
//				end.setPartyMember(BigDecimalUtil.integerAdd(end.getPartyMember(), vo.getPartyMember()));
//				end.setSupervisionObject(BigDecimalUtil.integerAdd(end.getSupervisionObject(), vo.getSupervisionObject()));
//				end.setWorkForce(BigDecimalUtil.integerAdd(end.getWorkForce(), vo.getWorkForce()));
//			}
//			data.add(end);
//		}
//		ReportReturnVO reportReturnVO = new ReportReturnVO();
//
//		ReportVO reportVO = new ReportVO();
//
//		reportVO
//				.addHeader(HeaderVO.builder().key("fillingUnitName").title("单位").build())
//				.addHeader(HeaderVO.builder().key("disciplineSecretary").title("纪委书记人数").build())
//				.addHeader(HeaderVO.builder().key("deputyDisciplineSecretary").title("纪委副书记人数").build())
//				.addHeader(HeaderVO.builder().key("otherCharge").title("其他分管").build())
//				.addHeader(HeaderVO.builder().key("disciplineFullTime").title("纪检专职").build())
//				.addHeader(HeaderVO.builder().key("disciplinePartTime").title("兼职纪检").build())
//				.addHeader(HeaderVO.builder().key("subtotal").title("小计").build())
//				.addHeader(HeaderVO.builder().key("cityTubeCadre").title("市管干部").build())
//				.addHeader(HeaderVO.builder().key("teamMembers").title("班子成员").build())
//				.addHeader(HeaderVO.builder().key("corporateMiddle").title("企业中层").build())
//				.addHeader(HeaderVO.builder().key("partyMember").title("中共党员").build())
//				.addHeader(HeaderVO.builder().key("supervisionObject").title("监察对象").build())
//				.addHeader(HeaderVO.builder().key("workForce").title("职工总数").build());
//		reportVO.setData(data);
//		reportReturnVO.setGrids(reportVO);
//		return reportReturnVO;
//	}
//
//	@Override
//	public void goBackToStash(String formId) {
//		handleStatus(Long.valueOf(formId), UNREVIEWED);
//	}
//
//	/**
//	 * 统一处理状态机事件方法
//	 *
//	 * @param id          主键
//	 * @param updateStatus  更新状态
//	 * @return boolean
//	 */
//	private Boolean handleStatus(Long id, int updateStatus) {
//		try {
//			DisciplineStatistics disciplineStatistics = getById(id);
//			/**
//			 * 设置审批人和审批时间
//			 */
//			if (updateStatus == FormStatusConstant.FINISH){
//				disciplineStatistics.setApprovedUser(TokenUtil.getTokenUserId());
//				Date now = DateUtil.now();
//				disciplineStatistics.setApprovedTime(now);
//			}
//			disciplineStatistics.setStatus(updateStatus);
//			updateById(disciplineStatistics);
//			return true;
//		} catch (Exception e) {
//			log.error(ExceptionUtils.getFullStackTrace(e));
//			return false;
//		}
//	}
//
//}
